import { defineStore } from "pinia";
import { ref } from "vue"; // 用户模块
import { userGetInfoService } from "../../api/user";

// token 管理
export const useUserStore = defineStore(
  "big-user",
  () => {
    const token = ref("");

    const setToken = (newToken) => {
      token.value = newToken;
    };
    const user = ref({});

    const getUser = async () => {
      const res = await userGetInfoService(); // 请求获取数据
      console.log("Pinia模块的userGetInfoService", res);
      setUser(res.data); // 存储用户数据
    };

    const setUser = (obj) => {
      user.value = obj; // 设置用户信息
    };
    const clearUser = () => {
      setUser({}); // 清空用户信息
      setToken(""); // 清空 token
      // 可选：手动清除 localStorage（确保完全清理）
      // localStorage.removeItem('big-user');
    };

    return {
      token,
      setToken,
      user,
      getUser,
      setUser,
      clearUser,
    };
  },
  {
    persist: {
      paths: ["token", "user"],
    },
  }
);
